package com.zenika.zenbinding.sample.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.zenika.zenbinding.client.ZenBinder;
import com.zenika.zenbinding.client.model.ListModel;
import com.zenika.zenbinding.client.model.ValueModel;
import com.zenika.zenbinding.client.widget.MultiColumnUserFormPanel;
import com.zenika.zenbinding.sample.model.Category;
import com.zenika.zenbinding.sample.model.Dialect;
import com.zenika.zenbinding.sample.model.Language;
import com.zenika.zenbinding.sample.model.LevelEnum;
import com.zenika.zenbinding.sample.model.Person;

/**
 * This time using a real panel/layout.
 * 
 * @author Pierre Queinnec
 */
public class FormulairePanel3 extends VerticalPanel {

	private Person person;
	private List categories;
	private List languages;
	private List dialects;

	private ListModel dialectModel;

	public FormulairePanel3() {

		this.setUp();

		MultiColumnUserFormPanel formPanel = new MultiColumnUserFormPanel(2, 6);
		formPanel.addTextBox(this.person, "name", "Nom :");
		formPanel.addTextBox(this.person, "firstName", "Prénom :");
		formPanel.addTextBox(this.person, "age", "Age :");
		formPanel.addCheckBox(this.person, "bachelor", "Célibataire :");
		String[] sexes = { "Homme", "Femme" };
		formPanel.addRadioGroup(this.person, "sex", sexes, null, false);

		ValueModel birthdayModel = formPanel.addTextBox(this.person, "birthday", "Date de naissance :");
		TextBox birthdayTB = (TextBox) birthdayModel.getWidget();
		birthdayTB.setMaxLength(10);
		birthdayTB.setVisibleLength(10);

		formPanel.addTextArea(this.person, "comment", "Remarques :");
		formPanel.addListBox(this.person, "category", this.categories, "name", "Catégorie :");
		ListModel languageModel = formPanel.addListBox(this.person, "language", this.languages, "name", "Langue :");
		((ListBox) languageModel.getWidget()).addChangeListener(new ChangeLanguageListener());
		// Last element to insert - widgetIndex 9
		dialectModel = formPanel.addListBox(this.person, "dialect", new ArrayList(), "name", "Speaking Dialect :");
		formPanel.addEnumListBox(this.person, "level", LevelEnum.getLevels(), "Level: ");

		this.add(formPanel);
	}

	private void updateDialectsList(Language languageSelected) {

		// get the widget
		ListBox listBox = (ListBox) this.dialectModel.getWidget();

		// clear this ListBox, in case some values are already filled in
		listBox.clear();

		// List containing all the Dialect BusinessObject with a languageId
		// equals to the languageSelected.getId()
		// selected
		List dialectsByLanguage = new ArrayList();
		dialectsByLanguage.add(new Dialect(-1, "Please select a dialect", languageSelected.getId()));

		// fill dialectsByLanguage with the relevant Dialect BusinessObject
		for (int i = 0; i < this.dialects.size(); i++) {
			Dialect currentDialect = (Dialect) this.dialects.get(i);
			if (currentDialect.getLanguageId() == languageSelected.getId())
				dialectsByLanguage.add(currentDialect);
		}

		// rebind ListBox with new set of values
		ZenBinder.bindListBox(listBox, this.person, "dialect", dialectsByLanguage, "name");
	}

	public class ChangeLanguageListener implements ChangeListener {

		public void onChange(Widget sender) {
			ListBox languages = (ListBox) sender;
			int selectedIndex = languages.getSelectedIndex();
			Language languageSelected = (Language) FormulairePanel3.this.languages.get(selectedIndex);
			FormulairePanel3.this.updateDialectsList(languageSelected);
		}
	}

	private void setUp() {

		this.categories = new ArrayList();
		this.categories.add(new Category("Technicien"));
		this.categories.add(new Category("Agriculteur"));
		this.categories.add(new Category("Cadre"));
		this.categories.add(new Category("Cadre Sup"));

		this.languages = new ArrayList();
		this.languages.add(new Language(-1, "Please select a language"));
		this.languages.add(new Language(1, "Français"));
		this.languages.add(new Language(2, "Chinois"));

		this.dialects = new ArrayList();
		this.dialects.add(new Dialect(1, "Cantonais", 2));
		this.dialects.add(new Dialect(2, "Alsacien", 1));
		this.dialects.add(new Dialect(3, "Mandarin", 2));
		this.dialects.add(new Dialect(4, "Breton", 1));
		this.dialects.add(new Dialect(5, "Parisien", 1));

		this.person = new Person("Durand", "Jean-Pierre");
		this.person.setAge(32);
		this.person.setBirthday(new Date());
		this.person.setCategory((Category) this.categories.get(2));
		this.person.setSex("Homme");
	}

}
